NPE when serializing a Timestamp field with custom date-time formatter#577
NPE when serializing a Timestamp field with custom date-time formatter#577greek1979 wants to merge 3 commits intoeclipse-ee4j:masterfrom
Conversation
5c90000 to
fc0a724
Compare
|
Sorry about a tab character that failed the prebuild checks; replaced it with whitespaces now. |
|
Hi @greek1979 , thank you for providing this patch :-) May I ask you please to also include test, so we can prevent regression? |
fc0a724 to
5d68bfa
Compare
|
TL;DR: unit test added. @Verdent , thank you for pointing this out! Writing a simple test case proved to be an enlightening experience, in fact. As I discovered that an |
5d68bfa to
3e482ef
Compare
12d9fac to
dbf3e58
Compare
|
Rebased feature branch from master to fix all the copyright errors that Checkstyle complained about yesterday... |
dbf3e58 to
e36b2b1
Compare
d1aa9e3 to
cb7093e
Compare
|
Updated this old pull request so the copyright year would comply with Checkstyle strict requirements... |
cb7093e to
da66070
Compare
da66070 to
10d565a
Compare
|
Updated the copyrights in all three affected source files again to indicate current year, 2025. |
|
Any thoughts on this PR? Or otherwise I will soon need to bump up [again] the copyright text to 2026 .. :-) |
|
This change looks good to me and seems to be very in demand. My review is non-binding (for now 🤞🏼) |
While working on an unrelated project, I found an issue in Yasson when trying to serialize a POJO with a SQL Timestamp field with a custom date-time formatter enabled in Yasson configuration. The default formatters works just fine, but any explicitly specified formatter produces an NPE in
SqlTimestampSerializer::formatWithFormattermethod as it callsAbstractDateSerializer::toTemporalAccessormethod which simply casts its argument to aTemporalAccessor. This does not work with legacy java.sql.Timestamp object, however, as Timestamp does not implement a TemporalAccessor read-only interface.Here is a small patch to the
SqlTimestampSerializerclass to convert a Timestamp into aLocalDateTime(Java 8 Date API) as both hold the same moment-in-time value, UTC time zone, so no information loss occurs. Resulting temporal field cannot be still serialized to JSON with a custom date-time format that includes unsupported elements such as time zone, for example, as neither SQLTimestampnorLocaDateTimecontain any time zone information. This is expected and is by design (of a rather messyjava.sql.Timestampclass).